/**
 * 
 */
package top.zyblogs.spider;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.FastArrayList;
import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;

import top.zyblogs.spider.film.dao.FilmMapper;
import top.zyblogs.spider.film.entity.Film;
import top.zyblogs.spider.utils.SpiderUtil;
import top.zyblogs.spider.utils.StringUtil;
import top.zyblogs.spider.utils.TxtUrlUtil;
import top.zyblogs.spider.utils.TxtUtil;
import us.codecraft.webmagic.Spider;

/**
 * @author zhaoyibo
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-cfg.xml")
public class TestJunit {
	public static StringBuffer urls=new StringBuffer();
	public static String flagType="";
	@Autowired
	private TxtUrlUtil txtUrlUtil;
	@Autowired
	private TxtUtil txtUtil;
	
	@Test
	public void createTxt() {
		String siteUrl="https://www.506zh.com";
		String[] filePaths= {/*"D:\\littleXY.txt","D:\\littleMM.txt",*/"D:\\littleLL.txt"};
		for (int i = 0; i < filePaths.length; i++) {
			if (i==0) {
				flagType="校园";
			}else if (i==1) {
				flagType="人妻";
			}else if (i==2) {
				flagType="乱伦";
			}
			File file=new File("D:\\xiaoshuo\\"+flagType);
			if (!file.exists()) {
				file.mkdirs();
			}
			String[] split = StringUtil.readAText(new File(filePaths[i])).split("\r\n");
			for (String string : split) {
				if (!StringUtils.isEmpty(string)) {
					Spider.create(txtUtil).addUrl(siteUrl+string).thread(5).run();
				}
			}
		}
	}
	@Test
	public void getTxtUrl() {
		String renqi="https://www.506zh.com/html/news/45/";
		String luanlun="https://www.506zh.com/html/news/46/";
		String xiaoyuan="https://www.506zh.com/html/news/47/";
		
		for(int i=1;i<=37;i++) {
			if (i==1) {
				Spider.create(txtUrlUtil).addUrl(xiaoyuan).thread(5).run();
			}else {
				Spider.create(txtUrlUtil).addUrl(xiaoyuan+i+".html").thread(5).run();
			}
		}
		
		FileWriter fw=null;
	    try {

	        fw=new FileWriter("D:\\littleXY.txt");
	        fw.write(urls.toString());

	    } catch (IOException e) {
	        e.printStackTrace();

	    }finally {  
	        try {
	            if(fw!=null)
	            fw.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	}
	
	public static void main(String[] args) {
		String readAText = StringUtil.readAText(new File("D:\\littleXY.txt"));
		String[] split = readAText.split("\n");
		File file=new File("D:\\nihao");
		if (!file.exists()) {
			file.mkdirs();
			
		}
	}
}